package org.example.common.utils;

import cn.hutool.core.collection.CollUtil;

import java.util.Collection;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * @author weigang
 * @create 2022-10-27 16:21
 */
public class StreamUtils {
    public static <T> Stream<T> ofStream(Collection<T> col) {
        if (CollUtil.isEmpty(col)) {
            return Stream.empty();
        }

        return col.stream();
    }


    /**
     * 获取默认值
     */
    public static <T, R> R defaultIfNull(T target, Function<T, R> mapper, R defaultValue) {
        // Optional每次都会新建对象，不太适合简单场景
        if (Objects.isNull(target)) {
            return defaultValue;
        }

        return mapper.apply(target);
    }
}
